import json
from datetime import datetime

class GlBookBalanceRequestParams:
    """
    用于构建财务余额表查询请求参数的类
    """

    # 固定参数（按实际请求参数调整）
    BASE_PARAMS = {
        "init_date_start": "2025-08-01T00:00:00.000Z",
        "showSubtotal": "false",
        "endAccountGrade": 5,
        "noOccurNotShow": "false",
        "showZero": "true",
        "showBothZero": "false",
        "printType": 0,
        "currencyId": "0",
        "showStopAccount": "true",
        "showAuxAccCalc": "true",
        "beginAccountCode": "",
        "beginAccountGrade": 1,
        "showMonthDetail": "false",
        "showSpec": "true",
        "init_date_end": "2025-08-01T00:00:00.000Z",
        "onlyShowEndAccount": "false",
        "noEndAmountNotShow": "false",
        "endAccountCode": "",
        "beginDate": "",
        "beginYear": "",
        "beginPeriod": "",
        "endDate": "",
        "endYear": "",
        "endPeriod": "",
        "isCalcMulti": False,
        "isCalcQuantity": False,
        "showSpecParam": "1",
        "pageSize": 1000000,
        "currentPage": 1,
        "needPaging": True
    }

    def __init__(self, beginDate: str):
        """
        初始化请求参数

        :param beginDate: 开始日期（格式: YYYY-MM，必填）
        """
        self.params = self.BASE_PARAMS.copy()
        self.set_begin_date(beginDate)

    def set_begin_date(self, beginDate: str):
        """
        设置开始日期（同时设置 beginYear 和 beginPeriod）

        :param beginDate: 开始日期（格式: YYYY-MM）
        """
        self.params["beginDate"] = beginDate
        year, period = beginDate.split('-')
        self.params["beginYear"] = year
        self.params["beginPeriod"] = period
        return self

    def set_end_date(self, endDate: str = None):
        """
        设置结束日期（默认使用 beginDate 作为 endDate）

        :param endDate: 结束日期（格式: YYYY-MM），若未提供则使用 beginDate
        """
        if endDate is None:
            endDate = self.params["beginDate"]
        self.params["endDate"] = endDate
        year, period = endDate.split('-')
        self.params["endYear"] = year
        self.params["endPeriod"] = period
        return self

    def set_begin_account_code(self, beginAccountCode: str):
        """
        设置开始科目代码

        :param beginAccountCode: 开始科目代码
        """
        self.params["beginAccountCode"] = beginAccountCode
        return self

    def set_end_account_code(self, endAccountCode: str):
        """
        设置结束科目代码

        :param endAccountCode: 结束科目代码
        """
        self.params["endAccountCode"] = endAccountCode
        return self

    def set_begin_account_grade(self, beginAccountGrade: int):
        """
        设置开始科目级别

        :param beginAccountGrade: 科目级别（整数）
        """
        self.params["beginAccountGrade"] = beginAccountGrade
        return self

    def set_end_account_grade(self, endAccountGrade: int):
        """
        设置结束科目级别

        :param endAccountGrade: 科目级别（整数）
        """
        self.params["endAccountGrade"] = endAccountGrade
        return self

    def set_page_size(self, pageSize: int):
        """
        设置分页大小

        :param pageSize: 每页记录数
        """
        self.params["pageSize"] = pageSize
        return self

    def set_current_page(self, currentPage: int):
        """
        设置当前页码

        :param currentPage: 当前页码
        """
        self.params["currentPage"] = currentPage
        return self

    def to_request_body(self):
        """
        构建完整的请求体

        :return: 包含 interfaceName, methodName, methodParam 的字典
        """
        method_param_str = json.dumps([self.params])

        return {
            "interfaceName": "com.ttk.gl.facade.IGlBookBalanceFacade",
            "methodName": "query",
            "methodParam": method_param_str
        }

    def __str__(self):
        """返回参数的字符串表示（用于调试）"""
        return f"GlBookBalanceRequestParams(beginDate={self.params['beginDate']}, endDate={self.params['endDate']})"